import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ileddemo/code/ui/base/BaseWidget.dart';
import 'package:lib_util/ToastUtil.dart';

import '../../../generated/l10n.dart';
import '../../ble/protocol/iledcolor/source/SourceManager.dart';
import '../../ble/protocol/iledcolor/source/SourceSendUtil.dart';
import '../../ble/protocol/iledcolor/source/text/CharBean.dart';
import '../../ble/protocol/iledcolor/source/text/TextSourceBean.dart';
import '../base/SendAndSaveWidget.dart';
import '../base/TitleBarWidget.dart';
//编辑文字节目
class EditTextPage extends StatefulWidget {
  final TextSourceBean sourceBean;
  const EditTextPage({super.key,  required this.sourceBean});

  @override
  State<EditTextPage> createState() => _EditTextPageState();
}

class _EditTextPageState extends State<EditTextPage> {
  List<String> mode = [];
  final FocusNode _focusNode = FocusNode();
  final TextEditingController _controller = TextEditingController();
  @override
  void initState() {
    _controller.text = widget.sourceBean.textContent;
    super.initState();
  }

  @override
  void dispose() {
    _focusNode.unfocus();
    super.dispose();
  }



  @override
  Widget build(BuildContext context) {
    if (mode.isEmpty) {
      mode.add(S.of(context).str_id_9);
      mode.add(S.of(context).str_id_10);
      mode.add(S.of(context).str_id_11);
      mode.add(S.of(context).str_id_12);
      mode.add(S.of(context).str_id_13);
      mode.add(S.of(context).str_id_14);
      mode.add(S.of(context).str_id_15);
      mode.add(S.of(context).str_id_16);
    }
    return BaseWidget(child: Column(children: [
      TitleBarWidget(title: S.of(context).str_id_18),
      const SizedBox(height: 20),
      //输入框
      Container(
          margin: const EdgeInsets.only(left: 10, right: 10),
          child: TextField(
              inputFormatters: [
                // 禁止输入换行符
                FilteringTextInputFormatter.deny(RegExp(r'\n'))
              ],
              onTapOutside: (event) {
                _focusNode.unfocus();
              },
              textInputAction: TextInputAction.done,
              focusNode: _focusNode,
              controller: _controller,
              onChanged: (value) async {
                String str = value;
                if (str.isNotEmpty) {
                  final List<String> splitText = str.characters.toList();
                  List<CharBean> content = [];
                  for (int i = 0; i < splitText.length; i++) {
                    var c = splitText[i];
                    CharBean b;
                    if (i < widget.sourceBean.content.length) {
                      b = widget.sourceBean.content[i];
                      b.content = c;
                    } else {
                      b = CharBean();
                      b.content = c;
                      b.color = widget.sourceBean.tmpColor.value;
                    }
                    content.add(b);
                  }
                  widget.sourceBean.content.clear();
                  widget.sourceBean.content.addAll(content);
                } else {
                  widget.sourceBean.content.clear();
                }
                widget.sourceBean.notifyListeners();
              },
              minLines: 2,
              maxLines: 4,
              maxLength: 500,
              style: const TextStyle(color: Colors.black),
              decoration: InputDecoration(
                  border: const OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10))),
                  hintText: S.of(context).str_id_19,
                  hintStyle: const TextStyle(color: Colors.grey)))),

      Expanded(child: Container(
        margin: const EdgeInsets.only(left: 10, right: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
            children: [

        //速度调节
        Text(S.of(context).str_id_6, style: TextStyle(fontSize: 16)),
        Slider(
          max: 20,
          min: 1,
          value: 21 - widget.sourceBean.speed.toDouble(),
          onChanged: (value) {
            setState(() => widget.sourceBean.speed = 20 - value.toInt());
          },
        ),

        //停留时间
        Text(S.of(context).str_id_7, style: TextStyle(fontSize: 16)),
        Slider(
          value: widget.sourceBean.dwellTime.toDouble(),
          min: 0.0,
          max: 255,
          onChanged: (double newValue) {
            setState(() => widget.sourceBean.dwellTime = newValue.toInt());
          },
        ),

        //特效
        Row(
          children: [
            Text(S.of(context).str_id_8, style: TextStyle(fontSize: 16)),
            Expanded(child: const SizedBox(width: 10)),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      widget.sourceBean.effects--;
                      if (widget.sourceBean.effects < 0) {
                        widget.sourceBean.effects = mode.length - 1;
                      }
                      setState(() {});
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Icon(Icons.arrow_back_ios_outlined),
                    ),
                  ),
                  Text(mode[widget.sourceBean.effects], style: TextStyle(fontSize: 16)),
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      widget.sourceBean.effects++;
                      if (widget.sourceBean.effects >= mode.length) {
                        widget.sourceBean.effects = 0;
                      }
                      setState(() {});
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Icon(Icons.arrow_forward_ios_outlined),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
        const SizedBox(height: 20),

        //保存和播放按
        SendAndSaveWidget(
            sendText: S.of(context).str_id_4,
            saveText: S.of(context).str_id_5,
            sendTap: () async {
              //发送
              SourceSendUtil.instance.sendSource(widget.sourceBean.castPartitionSourceBean());
            },
            saveTap: () async {
              //不能保存空的内容
              if(widget.sourceBean.content.isEmpty){
                ToastUtil.showToast(S.of(context).str_id_34);
                return;
              }
              //保存
              SourceManager.instance.saveSource(widget.sourceBean);
              Navigator.of(context).pop(true);
              ToastUtil.showToast(S.of(context).str_id_17);
            })
      ]),))
    ],));
  }
}
